package fdts.calendar.render;

/********************************************************
 * FDTS SS 2012. Calendar Layouter and iCalendar Imorter*
 * ******************************************************/

import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import prefuse.Constants;
import prefuse.render.LabelRenderer;
import prefuse.util.FontLib;
import prefuse.util.StringLib;
import prefuse.visual.VisualItem;

/**
 * this class extends LabelRenderer and implements a label renderer, 
 * that draws a label in the calendar application in CalendarVisualization.
 * 
 * @author agnieszka wojdecka and andreas hoermann
 *
 */
public class CalendarLabelRenderer extends LabelRenderer {

	public String getText(VisualItem item) {
        String s = null;
        if ( item.canGetString(m_labelName) ) {
            return item.getString(m_labelName);            
        }
        return s;
    }
	
	public CalendarLabelRenderer(String textField1) {
        super.setTextField(textField1);
    }
	
    private String computeTextDimensions(VisualItem item, String text,
            double size){
		// put item font in temp member variable
		m_font = item.getFont();
		// scale the font as needed
		if ( size != 1 ) {
		m_font = FontLib.getFont(m_font.getName(), m_font.getStyle(),
		        size*m_font.getSize());
		}
	
		FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(m_font);
		StringBuffer str = null;
	
		// compute the number of lines and the maximum width
		int nlines = 1, w = 0, start = 0, end = text.indexOf(m_delim);
		m_textDim.width = 0;
		String line;
		for ( ; end >= 0; ++nlines ) {
			w = fm.stringWidth(line=text.substring(start,end));
			// abbreviate line as needed
			if ( m_maxTextWidth > -1 && w > m_maxTextWidth ) {
				if ( str == null )
					str = new StringBuffer(text.substring(0,start));
				str.append(StringLib.abbreviate(line, fm, m_maxTextWidth));
				str.append(m_delim);
				w = m_maxTextWidth;
			} else if ( str != null ) {
				str.append(line).append(m_delim);
			}
			// update maximum width and substring indices
			m_textDim.width = Math.max(m_textDim.width, w);
			start = end+1;
			end = text.indexOf(m_delim, start);
		}
		w = fm.stringWidth(line=text.substring(start));
		// abbreviate line as needed
		if ( m_maxTextWidth > -1 && w > m_maxTextWidth ) {
			if ( str == null )
				str = new StringBuffer(text.substring(0,start));
			str.append(StringLib.abbreviate(line, fm, m_maxTextWidth));
			w = m_maxTextWidth;
		} else if ( str != null ) {
			str.append(line);
		}
		// update maximum width
		m_textDim.width = Math.max(m_textDim.width, w);
		
		// compute the text height
		m_textDim.height = fm.getHeight() * nlines;
		
		return str==null ? text : str.toString();
	}
    
    /**
     * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)
     */
    protected Shape getRawShape(VisualItem item) {
        m_text = getText(item);
        Image  img  = getImage(item);
        double size = item.getSize();
        
        // get image dimensions
        double iw=0, ih=0;
        if ( img != null ) {
            ih = img.getHeight(null);
            iw = img.getWidth(null);    
        }
        
        // get text dimensions
        int tw=0, th=0;
        if ( m_text != null ) {
        	 m_text = computeTextDimensions(item, m_text, size);
             th = m_textDim.height;
             tw = m_textDim.width; 
        }
        
        // get bounding box dimensions
        double w=0, h=0;
        switch ( m_imagePos ) {
        case Constants.LEFT:
        case Constants.RIGHT:
            w = tw + size*(iw +2*m_horizBorder
                   + (tw>0 && iw>0 ? m_imageMargin : 0));
            h = Math.max(th, size*ih) + size*2*m_vertBorder;
            break;
        default:
            throw new IllegalStateException(
                "Unrecognized image alignment setting.");
        }
        
        // get the top-left point, using the current alignment settings
        getAlignedPoint(m_pt, item, w, h, m_xAlign, m_yAlign);
        
        if ( m_bbox instanceof RoundRectangle2D ) {
            RoundRectangle2D rr = (RoundRectangle2D)m_bbox;
            rr.setRoundRect(m_pt.getX(), m_pt.getY(), w, h,
                            size*m_arcWidth, size*m_arcHeight);
        } else {
            m_bbox.setFrame(m_pt.getX(), m_pt.getY(), w, h);
        }
        return m_bbox;
    }
}
